package com.wangp.mywrite.s_netty.nio.ch2;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author tooStronger
 * @date 2023/3/16
 */
public class TestFilesWalkFileTree {

    public static void main(String[] args) throws IOException {
//        statistics();
//        delMultiDirAllFile();

        String source = "D:\\迅雷下载";
        String target = "D:\\迅雷下载_bak";
        copyAllFile(source,target);
    }

    private static void copyAllFile(String source, String target) throws IOException {
        Files.walk(Paths.get(source)).forEach(path -> {
            try {
                String targetName = path.toString().replace(source, target);
                // 是目录
                if (Files.isDirectory(path)) {

                    Files.createDirectory(Paths.get(targetName));
                }
                // 是文件
                else if (Files.isRegularFile(path)) {
                    Files.copy(path, Paths.get(targetName));
                }
            } catch (Exception e) {
                System.out.println("error! " + e);
            }
        });
    }

    private static void delMultiDirAllFile() throws IOException {
        Files.walkFileTree(Paths.get("D:\\testdir"), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println("=====》 进入 " + dir);
                return super.preVisitDirectory(dir, attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println(file.getFileName());
                Files.delete(file);
                return super.visitFile(file, attrs);
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                System.out.println("《===== 退出 " + dir);
                Files.delete(dir);
                return super.postVisitDirectory(dir, exc);
            }
        });
    }

    private static void statistics() throws IOException {
        AtomicInteger dirCount = new AtomicInteger(0);
        AtomicInteger fileCount = new AtomicInteger(0);

        Files.walkFileTree(Paths.get(".\\"), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println("=====》 " + dir);
                dirCount.incrementAndGet();
                return super.preVisitDirectory(dir, attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println("=====》 " + file);
                fileCount.incrementAndGet();
                return super.visitFile(file, attrs);
            }
        });
        System.out.println("dir count is  " + dirCount.get() + " file count is " + fileCount.get());
    }
}
